package com.xiaoxin.controller;

import com.xiaoxin.model.vo.PageResult;
import com.xiaoxin.service.CommentService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021-09-05-22:47
 **/
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentService commentService;

    /*
     * 发布评论
     * */
    @PostMapping
    public ResponseEntity publishComments(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentService.publishComments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    /*
     * 分页查询评论列表
     * */
    @GetMapping
    public PageResult findComments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "8") Integer pagesize,
                                   String movementId) {
        PageResult pageResult = commentService.findComments(page, pagesize, movementId);
        return pageResult;
    }

    /*
     * 评论点赞
     * */
    @GetMapping("/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") ObjectId publishId) {
        Integer likeCommentCount = commentService.likeComment(publishId);
        return ResponseEntity.ok(likeCommentCount);
    }

    /*
     * 取消评论点赞
     * */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") ObjectId publishId) {
        Integer likeCommentCount = commentService.dislikeComment(publishId);
        return ResponseEntity.ok(likeCommentCount);
    }

}
